<?php



class Verify
{

    public static $instance;
    public $key ;
    public $database;

    public function __construct($key)
    {
        include_once "Database.php";
        $this->key = $key;
        $this->database = Database::instance();
    }

    public static function instance($key)
    {
        return self::$instance ?? new self($key);
    }

    public function key(){

//        return $this->success();
        $ip = $this->getIp();
        $date = date('Y-m-d');
        // 如果没有填写key，则免费试用20次
        $free_count = 5;
        if (empty($this->key)){
            $res = $this->database->select("select * from user_count where ip = '{$ip}' and date = '{$date}'");
            if (empty($res)){
                $this->database->update("insert into user_count (ip,count,date) values('{$ip}',1,'{$date}')");
                return $this->success();
            }
            if ($res['count'] < $free_count ){
                $this->database->update("update user_count set count = count + 1 where id = '{$res['id']}'");
                $res = $this->database->select("select * from user_count where id = '{$res['id']}'");
                $having = $free_count - $res['count'];
                return $this->success('今日剩余免费次数:'.$having);
            }
            return $this->errorMsg('今日免费'.$free_count.'次数已用完，欢迎明天再来! www.134gpt.com');
        }


        $res = $this->database->select("select * from user_key where user_key = '{$this->key}' limit 1");
        if (empty($res)) {
            return $this->errorMsg('密钥不存在!');
        }
        if ($res['count'] <= 0 ){
            return $this->errorMsg('调用次数已用完!');
        }
        $date = date('Y-m-d H:i:s');
        if ($res['expire'] < $date){
            return $this->errorMsg('账号已过期!');
        }
        $this->dec();
        if ($res['type'] == 1){
            return $this->success('剩余次数'.($res['count'] - 1));
        }else{
//            $time = date('Y-m-d H:i:s',strtotime($res['expire']) - strtotime($date));
//            return $this->success('剩余时间'.$time);
            return $this->success();
        }

    }

    public function dec()
    {
        $this->database->update("update user_key set count = count - 1 where user_key = '{$this->key}'");
    }

    public function errorMsg($msg = '操作失败')
    {
        return json_encode(['success'=>false,'msg'=>$msg]);
    }

    public function success($msg='')
    {
        return json_encode(['success'=>true,'msg'=>$msg]);
    }

    public function changeKey()
    {
        session_start();
        $key = $_SESSION['system_app_key'];
        $res = $this->database->select("select * from app_key where status = 0");
        $this->database->update("update app_key set status = 1 where appkey = '{$key}'");
        if (empty($res['appkey'])){
            return $this->errorMsg();
        }else{
            $_SESSION['system_app_key'] = $res['appkey'];
            return $this->success();
        }
    }

    public function log($log)
    {
        $ip = $this->getIp();

        $res = $this->database->insert( "INSERT INTO log (text, ip) VALUES ('{$log}', '{$ip}')");
    }

    public function getIp()
    {
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            $ip = trim($ips[0]);
        } else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }

    public function getCount()
    {
        $res = $this->database->select("select * from user_key where user_key = '{$this->key}' limit 1");
        if (empty($res)) {
            return $this->success('密钥不存在');
        }
        if ($res['count'] <= 0 ){
            return $this->success('次数已用完');
        }
        $date = date('Y-m-d H:i:s');
        if ($res['expire'] < $date){
            return $this->success('账号已过期');
        }
        if ($res['type'] == 1){
            return $this->success('剩余'.($res['count']).'次');
        }else{
            return $this->success('');
        }
    }
}